/**
 * create by young (young2f@outlook.com) on 2024/4/21
 */
'use strict';
const base = require('../base/controllerBase');

class MaterialController extends base {
  async create() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      console.info('meterrr body', body);
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };

      ctx.validate(model, body);

      const result = await service.materialService.create(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async updateOne() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };

      ctx.validate(model, body);
      const result = await service.materialService.updateOne(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }
  async updateStoreCount() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };

      ctx.validate(model, body);
      const result = await service.materialService.updateStoreCount(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async deleteOne() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
      };

      ctx.validate(model, body);
      const result = await service.materialService.deleteOne(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findMany() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;

      const model = {
        tenantCode: { type: 'string' },
      };

      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service.materialService.findMany(body);

      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }
  async findOne() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;

      const model = {
        tenantCode: { type: 'string' },
      };

      ctx.validate(model, body);
      body.isDeleted = false;
      const result = await service.materialService.findOne(body);
      ctx.body = result;
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }
}

module.exports = MaterialController;
